package com.renu.jdependencyanalyser.window.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.renu.jdependencyanalyser.cons.ProjectCons;
import com.renu.jdependencyanalyser.core.ClassBorderGene;
import com.renu.jdependencyanalyser.core.ProjectInfo;
import com.renu.jdependencyanalyser.data.ProjectDataCenter;
import com.renu.jdependencyanalyser.freemarker.AbstractProcessor;
import com.renu.jdependencyanalyser.freemarker.ClsUMLProcessor;
import com.renu.jdependencyanalyser.model.ClassModel;
import com.renu.jdependencyanalyser.utils.MyFileUtil;
import com.renu.jdependencyanalyser.utils.MyProjectUtil;
import com.renu.jdependencyanalyser.visualizer.PlantUMLVisualizer;
import com.renu.jdependencyanalyser.utils.ImageDialogUtil;

import java.io.File;

/**
 *  DumbAware : Project View 右键菜单项只在右击 Java 文件时显示
 */
public class ClsBorderUMLAction extends AnAction  implements DumbAware {


    @Override
    public void actionPerformed(AnActionEvent e) {
        if ( MyProjectUtil.projectBasePath==null)
            return;
        // TODO: insert action logic here
        VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
        if (file!= null){
            String path = file.getPath();
            file:///Users/renu/Documents/high/paper/ProjectTest/src/main/java/org/example/soot
            if (!path.contains(".java")){
                return;
            }
            MyFileUtil.createOutDir(MyProjectUtil.projectBasePath + ProjectCons.OUT_CLS_DIR);
            String classSig = MyFileUtil.extractClassSigFromJavaPath(path);
            ProjectInfo instance = ProjectDataCenter.projectInfo;
            ClassModel classModel = instance.getClassModel(classSig);
            ClassBorderGene classBorderGene = new ClassBorderGene(instance, classModel);

            PlantUMLVisualizer plantUMLVisualizer = new PlantUMLVisualizer();
            AbstractProcessor processor = new ClsUMLProcessor(instance, classBorderGene);

            String outPutFilePath = MyProjectUtil.projectBasePath + ProjectCons.OUT_CLS_DIR+ File.separator +classSig +".png";
            plantUMLVisualizer.generateGraph(processor,outPutFilePath);
            ImageDialogUtil.showImage(outPutFilePath,"查看"+classSig +"关于第三方依赖的UML类图");



        }


        //System.out.println(e);
    }



    @Override
    public void update(AnActionEvent e) {
        // 通过Condition来限制菜单项显示的条件
        e.getPresentation().setVisible(checkCondition(e));
    }

    private boolean checkCondition(AnActionEvent e) {
        // 检查是否为 Java 文件
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        if (psiFile != null && psiFile.getName().endsWith(".java")) {
            // 如果是 Java 文件，则显示菜单项
            return true;
        }

        // 其他情况不显示菜单项
        return false;
    }
}
